//
//  BTViewPagerViewController.m
//  BaseTools
//
//  Created by 李挺哲 on 15/8/27.
//  Copyright (c) 2015年 ltz. All rights reserved.
//

#import "BTViewPagerViewController.h"
#import "BTSliderBar.h"
#import "Mytable.h"
@interface BTViewPagerViewController () <UIScrollViewDelegate>
@property (strong, nonatomic) BTSliderBar *slideBar;
@property(strong,nonatomic)UIScrollView* sliderView;
@property(strong,nonatomic)NSMutableArray *Tables;
@end

@implementation BTViewPagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationTitle:@"ViewPager" barTintColor:0x1E86CB titleColor:0xFFFFFF isTranslucent:NO isHidden:YES];
    [self showNavigationLeftButtonTitle:@"返回" tintColor:0xFFFFFF];
    
     self.automaticallyAdjustsScrollViewInsets = NO;
    self.selectIndex=0;
    _Tables=[NSMutableArray array];
        [self setupSlideBar];
    self.sliderView=[[UIScrollView alloc] initWithFrame:CGRectMake(0,64, ScreenWidth, ScreenHeight)];
    _sliderView.backgroundColor=[UIColor redColor];
    NSLog(@"_sliderView  height =---%@",  NSStringFromCGRect(_sliderView.frame));
    [self.view addSubview:_sliderView];
    Mytable *ta1=[[Mytable alloc] init];
    Mytable *ta2=[[Mytable alloc] init];
    Mytable *ta3=[[Mytable alloc] init];
    Mytable *ta4=[[Mytable alloc] init];
    
    Mytable *ta5=[[Mytable alloc] init];
    
    Mytable *ta6=[[Mytable alloc] init];
    
    [_Tables addObject:ta1];
    [_Tables addObject:ta2];
    [_Tables addObject:ta3];
    [_Tables addObject:ta4];
    [_Tables addObject:ta5];
    [_Tables addObject:ta6];
    
    

    for (int i=0; i<_Tables.count; i++) {
        Mytable *ta=_Tables[i];
        CGRect newFrame=ta.frame;
        newFrame.size.width=ScreenWidth;
        newFrame.size.height=CGRectGetHeight(self.sliderView.frame);
        newFrame.origin.x=i*CGRectGetWidth(self.sliderView.frame);
        newFrame.origin.y=0;
        ta.frame=newFrame;
  
        [self.sliderView addSubview:ta];
        
  
        
    }
    
    [ta1 loadData];
     NSLog(@"_sliderView  height =---%@",  NSStringFromCGRect(ta1.frame));
    
    
    self.sliderView.delegate=self;
    self.sliderView.pagingEnabled=YES;
    self.sliderView.bounces=NO;
    self.sliderView.contentSize=CGSizeMake(CGRectGetWidth(self.sliderView.frame)*self.Tables.count, CGRectGetHeight(self.sliderView.frame));
    self.sliderView.showsHorizontalScrollIndicator=NO;
    
    
    UIButton * button=[[UIButton alloc] init];
    button.frame=CGRectMake(0, 0, 80, 40);
    button.center=CGPointMake(ScreenWidth/2, ScreenHeight/2);
    [self.view addSubview:button];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    button.backgroundColor=[UIColor orangeColor];
    [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)click{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBar.hidden=NO;
    self.navigationController.navigationBar.translucent=YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// Set up a slideBar and add it into view
- (void)setupSlideBar {
    BTSliderBar *sliderBar = [[BTSliderBar alloc] init];
    sliderBar.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:128 / 255.0 blue:128 / 255.0 alpha:1.0];
    
    // Init the titles of all the item
    sliderBar.itemsTitle = @[@"要闻", @"视频", @"上海", @"娱乐", @"体育NBA", @"财经"];
    
    
    // Set some style to the slideBar
    sliderBar.itemColor = [UIColor whiteColor];
    sliderBar.itemSelectedColor = [UIColor orangeColor];
    sliderBar.sliderColor = [UIColor orangeColor];
    
    // Add the callback with the action that any item be selected
    [sliderBar slideBarItemSelectedCallback:^(NSUInteger idx) {
        [self silderWithIndex:idx isNeedScroller:NO];
        
    }];
    [self.view addSubview:sliderBar];
    _slideBar = sliderBar;
}
-(void)silderWithIndex:(NSInteger)index isNeedScroller:(BOOL)isNeed{
    
    _selectIndex=index;
    
    
    Mytable *ta=_Tables[_selectIndex];
    [ta loadData];
    NSLog(@"silderWithIndex  ---%ld",(long)_selectIndex);
    
    if (isNeed) {
        [_slideBar selectSlideBarItemAtIndex:_selectIndex];
        
        
    }
    else{
        
        
        [self.sliderView setContentOffset:CGPointMake(ScreenWidth*index, 0) animated:YES];
        
    }
    
    
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"_sliderView count %lu",(unsigned long)_sliderView.subviews.count);
    
    
    
    if ([scrollView isEqual:self.sliderView]) {
        NSInteger index=scrollView.contentOffset.x/ScreenWidth;
        
        if (index!=self.selectIndex) {
            NSLog(@"Index  ---%ld",(long)index);
            
            [self silderWithIndex:index isNeedScroller:YES];
        }
    }
}


@end
